package com.example.xmh.emo.mysql.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.xmh.emo.mysql.system.entity.PokemonClick;
import com.example.xmh.emo.mysql.system.mapper.PokemonClickMapper;
import com.example.xmh.entity.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xmh
 * @since 2021-11-17
 */
@RestController
@RequestMapping("/hook")
public class HookController {
    @Autowired
    PokemonClickMapper mapper;

    @GetMapping("/pokemon/click")
    public Resp<Boolean> getPokemonClickTimes(@RequestParam String name){
        PokemonClick pokemonClick = mapper.selectOne(new QueryWrapper<PokemonClick>().eq("name",name));

        if (pokemonClick==null){
            mapper.insert(new PokemonClick(name,1));
        }else {
            mapper.update(null,new UpdateWrapper<PokemonClick>().eq("name",name).set("times",pokemonClick.getTimes()+1));
        }

        return new Resp<>(200,"success",true);
    }

    @GetMapping("/pokemon/all")
    public Resp<Object> getPokemonAll(@RequestParam String id){
        List<PokemonClick> pokemonClicks = mapper.selectList(null);
        return new Resp<>(200,"success",pokemonClicks);
    }
}
